package com.iterator.controller.wechat.lc;

import com.iterator.bean.web.pojo.BaseRespVo;

import com.iterator.bean.wechat.lc.SearchIndexVO;
import com.iterator.mapper.web.userManager.MallUserMapper;
import com.iterator.service.wechat.lc.SearchServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * wx搜索模块 by homePage
 */
@RestController
@RequestMapping("wx/search")
public class SearchController {
    @Autowired
    SearchServiceImpl searchService;
    @Autowired
    MallUserMapper userMapper;

    /**
     * 搜索界面关键词/搜索历史
     * @return
     */
    @RequestMapping("index")
    public BaseRespVo index(){
        Object o = SecurityUtils.getSubject().getPrincipal();
        String username = (String) o;
        SearchIndexVO indexVO = searchService.getIndex(username);
        return BaseRespVo.ok(indexVO);
    }
    @RequestMapping("clearhistory")
    public BaseRespVo clearSearchHistory(){
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        searchService.clearSearchHistory(username);
        return BaseRespVo.ok();
    }

    /**
     * TODO 排序规则应该是同时包含所有keyword>包含部分keyword>只有单个keyword,两种内部再按照sort_order排序
     * @param keyword
     * @return
     */
    @RequestMapping("helper")
    public BaseRespVo helper(@RequestParam("keyword") String keyword){
        List<String> keywordList = searchService.getHelper(keyword);
        return BaseRespVo.ok(keywordList);
    }

}
